
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;

import logger.PoolLogger;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import utility.Input;

public class Uservalidation 
{	
	
	GetIdName gid ;
	String id;
	//SearchRecord sr;
	RecordSearch ps ;
	//final String path="C://Reports//Basic_Reports//Master_Reports//";
	//final String FILEFORMAT=".txt";
	final String ZERO="0";
	String TYPE="Type";
	String PASSWORD="Password";
	String USER="user";
	int index_pv,index_ref,index_vt,index_pid;
	//String input_path;
	PoolLogger pl;
	
	public Uservalidation() 
	{
		gid = new GetIdName();
		TYPE = gid.getId(TYPE);
		PASSWORD = gid.getId(PASSWORD);
		USER = gid.getId(USER);
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public boolean validate_User(String user,String password)
	{
		System.out.println("User name ->"+user+"\nPassword ->"+password);
		pl.info("User name ->"+user+"\nPassword ->"+password);
		pl.debug("User name ->"+user+"\nPassword ->"+password);
		pl.warn("User name ->"+user+"\nPassword ->"+password);
		pl.fatal("User name ->"+user+"\nPassword ->"+password);
		pl.error("User name ->"+user+"\nPassword ->"+password);
		pl.trace("User name ->"+user+"\nPassword ->"+password);
		
		id = gid.getId(user);
		if(id.equals("no$") || !gid.getItem(id).equals(user)) 
			return false;
		Hashtable<Object, Object> hasht = new Hashtable<Object, Object>();
		hasht.put("mid",id);
		//input_path = Input.MASTERREPORTSPATH + id + "//" + id + "_mid" + Input.FILRFORMAT;
		/*sr = new SearchRecord(input_path);
		sr.fileOpen();
		String line[] = sr.getRecordSet(hasht);
		sr.fileClose();*/
		hasht.put("td","null");
		ps = new RecordSearch();
		ps.setConditionMap(hasht);
		String line[] = ps.getArrayFromResultSet();
		if(validate_User(user)==true)
			for(int i=0;i<line.length;i++)
			{
			String Split[] =  line[i].split("\t");
				if(Split[index_pid].equals(PASSWORD))
				{
					if(Split[index_pv].equals(ZERO))
					{
						if(Split[index_vt].equals(password))
							return true;
						else
							return false; 
					}
					else
					{	
						if(gid.getItem(Split[index_pv]).equals(password))
							return true;
						else
							return false;
					}	
				}
			}
		return false;
	}
	
	public String getPasword(String User)
	{
		String password="";
		id = gid.getId(User);
		Hashtable<Object, Object> hasht = new Hashtable<Object, Object>();
		hasht.put("pid",PASSWORD);
		hasht.put("mid", id);
		//String input_path = Input.MASTERREPORTSPATH + id + "//" + id + "_mid" + Input.FILRFORMAT;
		/*sr = new SearchRecord(input_path);
		sr.fileOpen();
		String line[] = sr.getRecordSet(hasht);
		sr.fileClose();*/
		hasht.put("td","null");
		ps = new RecordSearch();
		ps.setConditionMap(hasht);
		String line[] = ps.getArrayFromResultSet();
		try 
		{
			//RandomAccessFile  raf = new RandomAccessFile(input_path,"r");
			//FileUtility fu = new FileUtility(raf);
			int index_vt = Column.vt_index-1;
			//raf.close();
			for(int i=0;i<line.length;i++)
			{
				String Split[] =line[i].split("\t");
				password = Split[index_vt];
			}
		}
		catch (Exception e) 
		{
			System.out.println("FileNotFoundException in display::Uservalidation::getpassword() "+e.getMessage());
			pl.info("FileNotFoundException in display::Uservalidation::getpassword() "+e.getMessage());
			pl.debug("FileNotFoundException in display::Uservalidation::getpassword() "+e.getMessage());
			pl.warn("FileNotFoundException in display::Uservalidation::getpassword() "+e.getMessage());
			pl.fatal("FileNotFoundException in display::Uservalidation::getpassword() "+e.getMessage());
			pl.error("FileNotFoundException in display::Uservalidation::getpassword() "+e.getMessage());
			pl.trace("FileNotFoundException in display::Uservalidation::getpassword() "+e.getMessage());
			e.printStackTrace();
		} 
		return password;
	}
	/**
	 * 
	 * @param username
	 * @return  boolean
	 */
	public boolean validate_User(String username)
	{
	boolean FLAG=false;
		try 
		{
			id = gid.getId(username);
			if(id.equals("no$"))
			{
				return false;
			}
			//input_path = Input.MASTERREPORTSPATH + id + "//" + id + "_mid" + Input.FILRFORMAT;
			index_pid = Column.pid_index-1;
			index_pv = Column.pv_index-1;
			index_vt = Column.vt_index-1;
			Hashtable<Object, Object> hasht = new Hashtable<Object, Object>();
			hasht.put("mid",id);
			/*sr = new SearchRecord(input_path);
			sr.fileOpen();
			String line[] = sr.getRecordSet(hasht);
			sr.fileClose();*/
			hasht.put("td","null");
			ps = new RecordSearch();
			ps.setConditionMap(hasht);
			String line[] = ps.getArrayFromResultSet();
			for(int i=0;i<line.length;i++)
			{
				String Split[] =  line[i].split("\t");
				if(Split[index_pid].equals(TYPE))
				{
					if(Split[index_pv].equals(USER))
					{
						 FLAG = true;
						break;
					}
				}
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return FLAG;
	}
	
	public void addDefaultDictionary(String userid) 
	{
	   ResultSet rs = DBManager.getSelect("Select * from dictionary where userid='"+userid+"'");
		try 
		{
			rs.next();
			rs.getString("userid");
		}
		catch (SQLException e) 
		{
			DBManager.getUpdate("Update copy_dictionary set userid='"+userid+"'");
			DBManager.getInsert("Insert into dictionary  select * from copy_dictionary");
		}
	}
	
	public static void main(String[] args) 
	{
		Uservalidation val = new Uservalidation();
		//System.out.println("The validation flag is " + val.validate_User("mahendra", "mahendraxyz"));
		//System.out.println("The validation flag is " + val.validate_User("Mahendra", "mahendraxyz"));
		System.out.println("The User validation is " + val.getPasword("+919922930647"));
	
	}
}
